/*
 * (C) Copyright 2006-2010
 * urbetter.inc.
 */

/*
 * Misc boot support
 */
#include <common.h>
#include <command.h>

//#include "s5p_lcd.h"
#include "../drivers/video/s5p_lcd.h"

#define DEBUG_CMD_DRAW
#undef DEBUG_CMD_DRAW
#ifdef DEBUG_CMD_DRAW
#define dbg(x...)       printf(x)
#else
#define dbg(x...)       do { } while (0)
#endif

extern int getScreenWidth(void);
extern int getScreenHeight(void);
extern unsigned short * getFrameBufferBase(void);
extern const unsigned char fontdata_8x8[];

#define RGB2888(r, g, b)  (unsigned int)((r) << 16 | (g) << 8 | (b))
#define RGB2565(r, g, b)  (unsigned short)(((r) >> 3) << 11 | ((g) >> 2) << 5 | ((b) >> 3))

#ifdef CONFIG_LCD_COLOR_RGB888
#define RGB2MV(r, g, b) 		RGB2888(r, g, b)
#else
#define RGB2MV(r, g, b) 		RGB2565(r, g, b)
#endif

#define COLOR_INVALID		(RGB2MV(9, 1, 1))
static mv_color s_text_color = RGB2MV(58, 110, 165);
static mv_color s_text_color_bg = COLOR_INVALID;

static const unsigned char * getGlyph(unsigned char ascii)
{
	return fontdata_8x8 + ((int)ascii * 8);
}

int do_draw_text (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
	int x, y, w, h, strike, i, j, k;

	mv_color * data, * base;
	unsigned char str[256];		//256 characters buffer!
	unsigned char * glyph;

	if (argc < 4) 
	{
		printf ("Usage:\n%s\n", cmdtp->usage);
		return 1;
	}

	base = getFrameBufferBase();
	x = simple_strtoul(argv[1], NULL, 10);
	y = simple_strtoul(argv[2], NULL, 10);
	w = getScreenWidth();
	h = getScreenHeight();
	strike = w;

	str[0] = '\0';
	for(i = 3; i < argc; i++)
	{
		if(argv[i])
		{
			strcat(str, argv[i]);
			strcat(str, " ");
		}
	}


	printf ("uttext draw: %s\n", str);

	if(x > w || y > h) 
	{	
		printf ("out of range, x=%d, y=%d, width=%d, height=%d\n", x, y, w, h);
		return 1;
	}
	
	if(!argv[3]) 
	{
		printf ("no string input\n");
		return 1;
	}

	for(i = 0; str[i] != '\0'; i++)
	{
		x += 8;
		if(x > w - 8)
			break;	//draw end

		data = base + y * strike + x;
		glyph = getGlyph(str[i]);
		for(j = 0; j < 8; j++)
		{	
			unsigned char mask = 0x80;
			for(k = 0; k < 8; k++)
			{
				if(glyph[j] & mask)
				{	
					data[k] = s_text_color;
					dbg("#");
				}
				else
				{
					if(s_text_color_bg != COLOR_INVALID)
						data[k] = s_text_color_bg;					
					dbg(" ");
				}
				mask >>= 1;
			}
			data += strike;
			dbg("\n");
		}
	}
	return 0;
}

int do_set_text_color (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
	int r, g, b;
	if (argc < 4) 
	{
		printf ("Usage:\n%s\n", cmdtp->usage);
		return 1;
	}

	r = simple_strtoul(argv[1], NULL, 10);
	g = simple_strtoul(argv[2], NULL, 10);
	b = simple_strtoul(argv[3], NULL, 10);
	s_text_color = RGB2MV(r, g, b);
	printf ("set text color to [%d, %d, %d]\n", r, g, b);
	return 0;
}

int do_set_text_bg_color (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
	int r, g, b;
	if (argc < 4) 
	{
		printf ("Usage:\n%s\n", cmdtp->usage);
		return 1;
	}

	r = simple_strtoul(argv[1], NULL, 10);
	g = simple_strtoul(argv[2], NULL, 10);
	b = simple_strtoul(argv[3], NULL, 10);
	s_text_color_bg = RGB2MV(r, g, b);
	printf ("set text background color to [%d, %d, %d]\n", r, g, b);
	return 0;
}

U_BOOT_CMD(
	uttext, CONFIG_SYS_MAXARGS, 1,	do_draw_text,
	"uttext    - uttext [x] [y] [your text]\n",
	"draw text on screen, default font 8x8 ascii, color[58, 110, 165]\n"
);

U_BOOT_CMD(
	uttextcolor, CONFIG_SYS_MAXARGS, 1,	do_set_text_color,
	"uttextcolor   - uttextcolor [R] [G] [B]\n",
	"set text color in RGB\n"
);

U_BOOT_CMD(
	uttextcolorbg, CONFIG_SYS_MAXARGS, 1,	do_set_text_bg_color,
	"uttextcolorbg   - uttextcolorbg [R] [G] [B]\n",
	"set text background color in RGB\n"
);

